// ChartDrawingArea.cs
// 
// Copyright (C) 2008 Jean-André Santoni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Gtk;
using Cairo;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using ChartStyle;

namespace Astrognome
{
	public class ChartDrawingArea : DrawingArea
	{
		private Context ctx;
		public Chart Chart;
		private Hashtable ci;
		public MainWindow parent;
		string appdir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
		
		public ChartDrawingArea(MainWindow mainwindow, Chart chart) : base()
		{
			Chart = chart;
			parent = mainwindow;
			UpdateChartInfo();
		}
		
		public void UpdateChartInfo()
		{
			ci = Parser.Parse(Chart.AstrologOutput);
		}
		
		protected override bool OnExposeEvent (Gdk.EventExpose args)
	    {
			ctx = Gdk.CairoHelper.Create(args.Window);
			
			ctx.Color = new Color (1, 1, 1);
			ctx.Rectangle(0, 0, Allocation.Width, Allocation.Height);
			ctx.FillPreserve();
			ctx.Clip();
			ctx.Scale(Allocation.Width/600.0, Allocation.Height/600.0);				

			string cs = parent.Config.ChartStyle;
			Assembly assembly = Assembly.LoadFrom(appdir + "/modules/"+cs+"/"+cs+".dll");
			
			foreach(Type type in assembly.GetTypes())
			{
				if(type.Name == "ChartStyle")
				{
					IChartStyle ChartStyle = (IChartStyle) Activator.CreateInstance(type);
					ChartStyle.Draw(ctx, ci);
				}
			}
			
			return true;
	    }
	}
}
